package com.yidu.t299.dao.twj;

import com.yidu.t299.domain.twj.RoleTree;
import com.yidu.t299.domain.twj.SysRight;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

@Mapper
public interface SysRightMapper {
    /**
     * 根据主键删除权限信息
     * @param rightCode 权限编号
     * @return 影响的行数(1:成功)
     */
    @Delete("delete from sys_right " +
            "        where right_code = #{rightCode,jdbcType=VARCHAR}")
    int deleteByPrimaryKey(@Param("rightCode") String rightCode);

    /**
     * 根据主键批量删除权限信息
     * @param array 权限编号数组
     * @return 影响的行数(1:成功)
     */
    @Delete("<script> " +
            "        delete from sys_right where right_code in " +
            "        <foreach collection=\"array\" open=\"(\" close=\")\" separator=\",\" item=\"rightCode\"> " +
            "            #{rightCode,jdbcType=VARCHAR} " +
            "        </foreach> " +
            "</script>")
    int deleteBatch(String[] array);

    /**
     * 添加权限信息
     * @param sysRight 权限对象
     * @return 影响的行数(1:成功)
     */
    @Insert("insert into sys_right (right_code, right_parent_code, right_type, " +
            "            right_text, right_url, right_tip) " +
            "        values (#{rightCode,jdbcType=VARCHAR}, #{rightParentCode,jdbcType=VARCHAR}, #{rightType,jdbcType=VARCHAR}, " +
            "            #{rightText,jdbcType=VARCHAR}, #{rightUrl,jdbcType=VARCHAR}, #{rightTip,jdbcType=VARCHAR})")
    int insert(SysRight sysRight);

    /**
     * 批量添加角色权限信息
     * @return 影响的行数(1:成功)
     */
    @Insert("<script> " +
            "insert into sys_role_right (rf_role_id, rf_right_code) " +
            "        <foreach collection=\"array\" open=\" values\" separator=\",\" item=\"item\">\n" +
            "            (#{roleId}, #{item})\n" +
            "        </foreach> " +
            "</script> ")
    int insertBatchRoleRight(String[] array,Integer roleId);

    /**
     * 删除角色权限信息
     * @return 影响的行数(1:成功)
     */
    @Delete("delete from sys_role_right where rf_role_id=#{roleId} ")
    int deleteRoleRight(Integer roleId);

    /**
     * 根据主键查询权限信息
     * @param rightCode 权限编号
     * @return 权限对象
     */
    @Select("select right_code, right_parent_code, right_type, right_text, right_url, right_tip " +
            "   from sys_right " +
            "   where right_code = #{rightCode,jdbcType=VARCHAR}")
    @Results(id = "BaseResultMap",value = {
            @Result(id = true,column = "right_code",property = "rightCode",jdbcType = JdbcType.VARCHAR),
            @Result(column = "right_parent_code",property = "rightParentCode",jdbcType = JdbcType.VARCHAR),
            @Result(column = "right_type",property = "rightType",jdbcType = JdbcType.VARCHAR),
            @Result(column = "right_text",property = "rightText",jdbcType = JdbcType.VARCHAR),
            @Result(column = "right_url",property = "rightUrl",jdbcType = JdbcType.VARCHAR),
            @Result(column = "right_tip",property = "rightTip",jdbcType = JdbcType.VARCHAR)
    })
    SysRight selectByPrimaryKey(@Param("rightCode") String rightCode);

    /**
     * 查询父节点的信息
     * @param right_parent_code 父节点权限编号
     * @return 权限对象
     */
    @Select("select right_code, right_parent_code, right_type, right_text, right_url, right_tip " +
            "   from sys_right " +
            "   where right_code = #{right_parent_code,jdbcType=VARCHAR}")
    @ResultMap("BaseResultMap")
    SysRight selectByParentId(@Param("right_parent_code") String right_parent_code);

    /**
     * 根据所给属性值来修改其字段值
     * @param sysRight 权限对象
     * @return 影响的行数(1:成功)
     */
    @Update("<script> " +
            "update sys_right\n" +
            "        <set>\n" +
            "            <if test=\"rightParentCode != null\">\n" +
            "                right_parent_code = #{rightParentCode,jdbcType=VARCHAR},\n" +
            "            </if>\n" +
            "            <if test=\"rightType != null\">\n" +
            "                right_type = #{rightType,jdbcType=VARCHAR},\n" +
            "            </if>\n" +
            "            <if test=\"rightText != null\">\n" +
            "                right_text = #{rightText,jdbcType=VARCHAR},\n" +
            "            </if>\n" +
            "            <if test=\"rightUrl != null\">\n" +
            "                right_url = #{rightUrl,jdbcType=VARCHAR},\n" +
            "            </if>\n" +
            "            <if test=\"rightTip != null\">\n" +
            "                right_tip = #{rightTip,jdbcType=VARCHAR},\n" +
            "            </if>\n" +
            "        </set>\n" +
            "        where right_code = #{rightCode,jdbcType=VARCHAR}\n" +
            "</script> ")
    int update(SysRight sysRight);



    /**
     * 批量查询
     * @param sysRight 权限对象
     * @return 权限对象集合
     */
    @Select("<script> " +
            "       SELECT a.right_code, a.right_text, a.right_type, a.right_url, a.right_tip, a.right_parent_code" +
            "       , b.right_text parent_right_text " +
            "        FROM sys_right a " +
            "        <where> " +
            "            <if test=\"rightText != null and rightText != ''\"> " +
            "                a.right_text like concat('%',#{rightText},'%') " +
            "            </if> " +
            "            <if test=\"rightType != null and rightType != ''\"> " +
            "                and a.right_type = #{rightType} " +
            "            </if> " +
            "        </where> " +
            "</script> ")
    @Results(id = "findAllResultMap",value = {
            @Result(id = true,column = "right_code",property = "rightCode",jdbcType = JdbcType.VARCHAR),
            @Result(column = "right_parent_code",property = "rightParentCode",jdbcType = JdbcType.VARCHAR),
            @Result(column = "right_type",property = "rightType",jdbcType = JdbcType.VARCHAR),
            @Result(column = "right_text",property = "rightText",jdbcType = JdbcType.VARCHAR),
            @Result(column = "right_url",property = "rightUrl",jdbcType = JdbcType.VARCHAR),
            @Result(column = "right_tip",property = "rightTip",jdbcType = JdbcType.VARCHAR)
    })
    List<SysRight> select(SysRight sysRight);

    /**
     * 根据角色编号查询当前角色所拥有的权限列表
     *   参数userRoleId：要查询的角色id、
     *   参数type：菜单的类型。
     *	   type为Button：则要过滤按钮类型权限，动态菜单使用
     *     type为null：则查询所有的权限类型。权限鉴定使用
     * @param userRoleId 角色编号
     * @param rightType 权限类型
     * @return 权限对象类型
     */
    @Select("<script> " +
            "   SELECT DISTINCT a.right_code, a.right_text, a.right_type, a.right_url, a.right_tip, a.right_parent_code\n" +
            "   FROM sys_right a\n" +
            "   JOIN sys_role_right b ON a.right_code = b.rf_right_code\n" +
            "        <where>\n" +
            "            b.rf_role_id = #{userRoleId}\n" +
            "            <if test=\"rightType != null and rightType != ''\">\n" +
            "                and a.right_type!='Button'\n" +
            "            </if>\n" +
            "        </where>\n" +
            "        ORDER BY a.right_code\n" +
            "</script> ")
    @ResultMap("BaseResultMap")
    List<SysRight> selectByUserId(@Param("userRoleId") Integer userRoleId, @Param("rightType") String rightType);

    /////////////////////////////////////////////////////////////////////////////////////////

    /**
     * 先查询菜单根级目录，递归查找所有
     *
     * @return 父子级结构数据
     */
    @Select("SELECT * FROM sys_right s WHERE right_parent_code!='ROOT_MENU' and right_type='Folder'")
    @Results(id = "findAllRecursionMap",value = {
            @Result(id = true,column = "right_code",property = "id",jdbcType = JdbcType.VARCHAR),
            @Result(column = "right_text",property = "title",jdbcType = JdbcType.VARCHAR),
            @Result(column = "right_code",property = "children",javaType = List.class,
                    many = @Many(select = "com.yidu.t299.dao.twj.SysRightMapper.getMenuChildren"))
    })
    List<RoleTree> findAllRecursion();

    /**
     * 再利用上次查询结果集中column的值right_code做递归查询，查出所有子菜单
     */
    @Select("SELECT * FROM sys_right WHERE  right_parent_code=#{right_code}")
    @ResultMap("findAllRecursionMap")
    List<RoleTree> getMenuChildren(@Param("right_code") String right_code);


    /**
     * 查询指定角色的所有权限
     */
    @Select("SELECT rf_right_code FROM sys_role_right WHERE rf_role_id=#{roleId}")
    List<String> getAllRight(@Param("roleId") Integer roleId);


}